//
//  ZLBaseTableContentView.m
//
//  Created by Jiaozl on 2022/1/5.
//  Copyright © 2022 Jiaozl. All rights reserved.
//

#import "ZLBaseTableContentView.h"
#import "UIColor+Additions.h"
#import <Masonry/Masonry.h>

@interface ZLBaseTableContentCell : UITableViewCell
@property (nonatomic, strong) ZLBaseTableItem *model;
@property (nonatomic, strong) UILabel *contentLabel;

@end

@implementation ZLBaseTableContentCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setupUI];
        [self setupConstraints];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return self;
}

#pragma mark - Public Method

- (void)setupContentWithModel:(ZLBaseTableItem *)model {
    self.model = model;
    self.contentLabel.text = self.model.title;
}

#pragma mark - Private Method

- (void)setupUI {
    [self.contentView addSubview:self.contentLabel];
}

- (void)setupConstraints {
    [self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.mas_equalTo(8);
        make.right.bottom.mas_equalTo(-8);
    }];
}

#pragma mark - Getter

- (UILabel *)contentLabel {
    if (!_contentLabel) {
        _contentLabel = [[UILabel alloc] init];
        _contentLabel.textColor = [UIColor colorWithHex:0x987654];
        _contentLabel.numberOfLines = 0;
        _contentLabel.adjustsFontSizeToFitWidth = YES;
    }
    return _contentLabel;
}


@end

@interface ZLBaseTableContentView () <UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;   ///<  列表视图
@property (nonatomic, copy) NSArray <ZLBaseTableItem *> *datas;  ///<  数据源数组


@end

@implementation ZLBaseTableContentView

#pragma mark - Life Cycle

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self setupUI];
        [self setupConstraints];
    }
    return self;
}

- (void)dealloc {
    NSLog(@"Jiaozl %s", __func__);
}

#pragma mark - Public Method

- (void)updateDataSource:(NSArray<ZLBaseTableItem *> *)dataSource {
    if ([dataSource isKindOfClass:NSArray.class]) {
        self.datas = dataSource;
        [self.tableView reloadData];
    }
}

#pragma mark - Private Method

- (void)setupUI {
    [self addSubview:self.tableView];
}

- (void)setupConstraints {
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self);
    }];
}

#pragma mark - UITableViewDelegate & UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.datas.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ZLBaseTableItem *model = self.datas[indexPath.row];
    ZLBaseTableContentCell *cell;
    cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass(ZLBaseTableContentCell.class)];
    [cell setupContentWithModel:model];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 50;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    ZLBaseTableItem *model = self.datas[indexPath.row];
    if (model.block) {
        model.block(model);
    }
}

#pragma mark - Getter

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] init];
        [_tableView registerClass:ZLBaseTableContentCell.class forCellReuseIdentifier:NSStringFromClass(ZLBaseTableContentCell.class)];
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    return _tableView;
}

@end
